package com.youngball.web.controller.sys.web.controller;


import com.youngball.business.utill.Code;
import com.youngball.shiro.entity.Resource;
import com.youngball.shiro.service.ResourceService;
import com.youngball.shiro.service.RoleService;
import com.youngball.web.controller.BaseController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 */
@Controller
@RequestMapping("${adminPath}/resource")
public class ResourceController extends BaseController{

    @Autowired
    private RoleService roleService;

    @Autowired
    private ResourceService resourceService;

    @ModelAttribute("types")
    public Resource.ResourceType[] resourceTypes() {
        return Resource.ResourceType.values();
    }

    @RequiresPermissions("resource:view")
    @RequestMapping(method = RequestMethod.GET)
    public String list(Model model) {
        model.addAttribute("resourceList", resourceService.findAll());
        return "resource/list";
    }

    @RequestMapping(value = "resourceList",method = RequestMethod.GET)
    public ResponseEntity<Map> resourceList() {
        Map maps = new HashMap();
        try
        {
            Resource organization = null;
            List<Resource> list = resourceService.findByParent(Long.valueOf(0));
            if(list != null && list.size() > 0)
            {
                organization = list.get(0);
                List<Resource> organizations0 = resourceService.findByParent(organization.getId());
                for(Resource first : organizations0)
                {
                    List<Resource> organizations1 = resourceService.findByParent(first.getId());
                    if(organizations1 == null) continue;

                    for(Resource second : organizations1)
                    {
                        List<Resource> organizations2 = resourceService.findByParent(second.getId());
                        if(organizations2 == null) continue;

                        for(Resource third : organizations2)
                        {
                            List<Resource> organizations3 = resourceService.findByParent(third.getId());
                            if(organizations3 == null) continue;

                            third.setChildren(organizations3);
                        }
                        second.setChildren(organizations2);
                    }
                    first.setChildren(organizations1);
                }
                organization.setChildren(organizations0);
            }

            maps.put("code","0");
            maps.put("msg", "获取成功");
            maps.put("data",organization);
            return new ResponseEntity<Map>(maps, responseHeaders, HttpStatus.OK);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        maps.put("msg", "获取失败");
        maps.put("code", Code.DEFAI.toString());
        return new ResponseEntity<Map>(maps, responseHeaders, HttpStatus.OK);
    }

    @RequiresPermissions("resource:create")
    @RequestMapping(value = "/{parentId}/appendChild", method = RequestMethod.GET)
    public String showAppendChildForm(@PathVariable("parentId") Long parentId, Model model) {
        Resource parent = resourceService.findOne(parentId);
        model.addAttribute("parent", parent);
        Resource child = new Resource();
        child.setParentId(parentId);
        child.setParentIds(parent.makeSelfAsParentIds());
        model.addAttribute("resource", child);
        model.addAttribute("op", "新增子节点");
        return "resource/edit";
    }

    @RequiresPermissions("resource:create")
    @RequestMapping(value = "/{parentId}/appendChild", method = RequestMethod.POST)
    public String create(Resource resource, RedirectAttributes redirectAttributes) {
        resourceService.createResource(resource);
        redirectAttributes.addFlashAttribute("msg", "新增子节点成功");
        return "redirect:/resource";
    }

    @RequestMapping(value = "createResource",method = RequestMethod.POST)
    public ResponseEntity<Map> createResource(@RequestBody Resource resource) {
        Map maps = new HashMap();
        try
        {
            resourceService.createResource(resource);
            maps.put("code","0");
            maps.put("msg", "新增成功");
            return new ResponseEntity<Map>(maps, responseHeaders, HttpStatus.OK);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        maps.put("msg", "获取失败");
        maps.put("code", Code.DEFAI.toString());
        return new ResponseEntity<Map>(maps, responseHeaders, HttpStatus.OK);
    }

    @RequiresPermissions("resource:update")
    @RequestMapping(value = "/{id}/update", method = RequestMethod.GET)
    public String showUpdateForm(@PathVariable("id") Long id, Model model) {
        model.addAttribute("resource", resourceService.findOne(id));
        model.addAttribute("op", "修改");
        return "resource/edit";
    }

    @RequiresPermissions("resource:update")
    @RequestMapping(value = "/{id}/update", method = RequestMethod.POST)
    public String update(Resource resource, RedirectAttributes redirectAttributes) {
        resourceService.updateResource(resource);
        redirectAttributes.addFlashAttribute("msg", "修改成功");
        return "redirect:/resource";
    }

    @RequestMapping(value = "updateResource",method = RequestMethod.POST)
    public ResponseEntity<Map> updateResource(@RequestBody Resource resource) {
        Map maps = new HashMap();
        try
        {
            Resource local = resourceService.findOne(resource.getId());
            if(resource.getName() != null) local.setName(resource.getName());
            if(resource.getType() != null) local.setType(resource.getType());
            if(resource.getUrl() != null) local.setUrl(resource.getUrl());
            if(resource.getPermission() != null) local.setPermission(resource.getPermission());

            resourceService.updateResource(local);
            maps.put("code","0");
            maps.put("msg", "修改成功");
            return new ResponseEntity<Map>(maps, responseHeaders, HttpStatus.OK);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        maps.put("msg", "获取失败");
        maps.put("code", Code.DEFAI.toString());
        return new ResponseEntity<Map>(maps, responseHeaders, HttpStatus.OK);
    }

    @RequiresPermissions("resource:delete")
    @RequestMapping(value = "/{id}/delete", method = RequestMethod.GET)
    public String delete(@PathVariable("id") Long id, RedirectAttributes redirectAttributes) {
        resourceService.deleteResource(id);
        redirectAttributes.addFlashAttribute("msg", "删除成功");
        return "redirect:/resource";
    }

    @RequestMapping(value = "deleteResource/{id}",method = RequestMethod.POST)
    public ResponseEntity<Map> deleteResource(@PathVariable Long id) {
        Map maps = new HashMap();
        try
        {
       /*     Role users = roleService.findRoles(id);
            if(users != null)
            {
                maps.put("code","1");
                maps.put("msg", "该部门下还有用户，无法删除");
                return new ResponseEntity<Map>(maps, responseHeaders, HttpStatus.OK);
            }*/

            resourceService.deleteResource(id);
            maps.put("code","0");
            maps.put("msg", "删除成功");
            return new ResponseEntity<Map>(maps, responseHeaders, HttpStatus.OK);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        maps.put("msg", "获取失败");
        maps.put("code", Code.DEFAI.toString());
        return new ResponseEntity<Map>(maps, responseHeaders, HttpStatus.OK);
    }
}
